#include <bits/stdc++.h>
using namespace std;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
using ll = long long;
ll n;
struct pp
{
    string s1, s2;
    bool f1, f2;
} a[1010];
string sts;
vector<int> ans, res;
bool stf;
void dfs(ll dep)
{
    if (dep == 0)
    {
        for (ll i = 1; i <= n; i++)
        {
            sts = a[i].s1;
            stf = a[i].f1;
            res.push_back(i);
            dfs(dep + 1);
            res.pop_back();
        }
        return;
    }

    if (dep > n)
        return;
    if (!ans.empty() && res.size() > ans.size())
        return;

    ll b = res.back();
    if (sts == a[b].s2 && !(stf == a[b].f2))
    {
        if (ans.empty() || ans.size() > res.size())
            ans = res;
    }

    for (ll i = 1; i <= n; i++)
    {
        if (a[i].s1 == a[b].s2 && a[i].f1 == a[b].f2)
        {
            res.push_back(i);
            dfs(dep + 1);
            res.pop_back();
        }
    }
}
void solve()
{
    cin >> n;
    for (ll i = 1; i <= n; i++)
    {
        cin >> a[i].s1 >> a[i].f1 >> a[i].s2 >> a[i].f2;
    }
    if (n == 1)
    {
        ans.push_back(1);
    }
    else
        dfs(0);
    // cout << "debug" << endl;

    // cout << "debug" << endl;
    for (ll i = 00; i < ans.size(); i++)
    {
        ll now = ans[i];
        cout << a[now].s1 << " " << a[now].f1 << " " << a[now].s2 << " " << a[now].f2 << " ";
    }
    ll st = ans.front(), ed = ans.back();
    cout << "= " << a[st].s1 << " " << a[st].f1 << " " << a[ed].s2 << " " << a[ed].f2;
}
int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
